package com.example.service.impl;

import com.example.mapper.CompetitionMapper;
import com.example.pojo.*;
import com.example.service.CompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CompetitionServiceImpl implements CompetitionService {
    @Autowired
    private CompetitionMapper competitionMapper;

    @Override
    public void addCategory(CompetitionCategory competitionCategory) {
        competitionMapper.addCategory(competitionCategory);
    }

    @Override
    public void add(Competition competition) {
        competition.setCreatedAt(LocalDateTime.now());
        competition.setUpdatedAt(LocalDateTime.now());
        competitionMapper.add(competition);
    }

    @Override
    public List<Competition> list() {
        List<Competition> list= competitionMapper.list();
        return list;
    }

    @Override
    public void delete(Integer id) {
        competitionMapper.delete(id);
    }

    @Override
    public void update(Competition competition) {
        competition.setUpdatedAt(LocalDateTime.now());
        competitionMapper.update(competition);
    }

    @Override
    public Competition detail(Integer id) {
        Competition competition = competitionMapper.detail(id);
        return competition;
    }

    @Override
    public void join(Integer id, Integer userId) {
        CompetitionParticipant competitionParticipant = new CompetitionParticipant();
        competitionParticipant.setCompetitionId(id);
        competitionParticipant.setUserId(userId);
        competitionParticipant.setJoinTime(LocalDateTime.now());
        competitionMapper.join(competitionParticipant);
    }

    @Override
    public void cancel(Integer id, Integer userId) {
        competitionMapper.cancel(id,userId);
    }

    @Override
    public List<Competition> joined(Integer userId) {
        List<Competition> list = competitionMapper.joined(userId);
        return list;
    }

    @Override
    public void submit(Integer competitionId, Integer articleId, Integer userId) {
        Submission submission = new Submission(null,competitionId,  articleId, userId,LocalDateTime.now(),"提交",0);
        competitionMapper.submit(submission);
    }

    @Override
    public void score(Long submissionId, Integer score) {
        //根据submissionId 修改作品分数
        competitionMapper.score(submissionId,score);
    }

    @Override
    public Competition findByUserId(Integer userId, Integer competitionId) {
        Competition competition = competitionMapper.findByUserId(userId,competitionId);
        return competition;
    }

    @Override
    public Competition findBySubmissionId(Long submissionId) {
        Competition competition = competitionMapper.findBySubmissionId(submissionId);
        return competition;
    }

    @Override
    public List<CompetitionCategory> listCategory() {
        List<CompetitionCategory> list = competitionMapper.listCategory();
        return list;
    }

    @Override
    public void deleteCategory(Integer id) {
        competitionMapper.deleteCategory(id);
        competitionMapper.delete(id);
    }

    @Override
    public void updateCategory(CompetitionCategory competitionCategory) {
        competitionMapper.updateCategory(competitionCategory);
    }


}
